package com.tianqu.tms.www.settle.action;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TransferMonthReportForm;
import com.tianqu.tms.www.settle.actionform.TransferSettleActionForm;
import com.tianqu.tms.www.settle.viewhelper.TransferSettleViewHelper;

public class TransferStatisticsMonthSearchResultAction extends TmsAction {

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		TransferSettleActionForm inForm = (TransferSettleActionForm)form;
		TransferSettleViewHelper view = new TransferSettleViewHelper();
		//#时间不能大于上个月，否则页面提示“受理月份不能大于上个月！” 2014-01
		String acceptMonth = inForm.getAcceptMonth();
		String year = acceptMonth.substring(0, 4);
		String month = acceptMonth.substring(5, 7);
		String cYear = DateUtil.getCurrentYear();
		String cMonth = DateUtil.getCurrentMonth();
		if(Integer.parseInt(cYear)<Integer.parseInt(year))
		{
			//提示 受理月份不能大于上个月！
			setErrorsToRequest(request, TmsConstants.IM030, null);
			return mapping.getInputForward();
		}else{
			if(year.equals(cYear))
			{
				//当年
				if(Integer.parseInt(month)>=Integer.parseInt(cMonth))
				{
					//提示 受理月份不能大于上个月！
					setErrorsToRequest(request, TmsConstants.IM030, null);
					return mapping.getInputForward();
				}
			}
		}
		//验证通过 直接查出列表 根据年份和月份查询列表
		view.setAcceptMonth(acceptMonth);
		TransferMonthReportForm report = new TransferMonthReportForm();
		List list = report.getTransferMonthReportFormInfo(Integer.parseInt(year), Integer.parseInt(month));
		view.setReportList(list);
		TransferMonthReportForm report1 = new TransferMonthReportForm();
		report1 = report1.getTotleInfo(Integer.parseInt(year), Integer.parseInt(month), 0);
		if(report1!=null)
		{
			view.setSumQuantity(report1.getQuantity());
			view.setSumCountPrice(report1.getCountPrice());
		}else{
			view.setSumQuantity("");
			view.setSumCountPrice("");
		}
		TransferMonthReportForm report2 = new TransferMonthReportForm();
		report2 = report2.getTotleInfo(Integer.parseInt(year), Integer.parseInt(month), 1);
		if(report2!=null)
		{
			view.setSumJQuantity(report2.getQuantity());
			view.setSumJCountPrice(report2.getCountPrice());
		}else{
			view.setSumJQuantity("");
			view.setSumJCountPrice("");
		}
		request.setAttribute(TmsConstants.VIEWHELP, view);
		return mapping.findForward(TmsConstants.SUCCESS);
	}
}
